package com.runen.infinitemovement.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class SetTargetDialog {
    private Context context;
    private BaseCustomDialog dialog;
    private int type;
    private int selectInt;
    private List<String> list = new ArrayList<>();
    private int min = 0;
    private int max = 100;
    private int gradient = 0;
    private int selectIndex = 0;
    private OnSelect onSelect;

    public void setOnSelect(OnSelect onSelect) {
        this.onSelect = onSelect;
    }

    public SetTargetDialog(Context context, int type, int selectInt) {
        this.context = context;
        this.type = type;
        this.selectInt = selectInt;
        switch (type) {
            case 1:
                min = 1000;
                max = 50 * 1000;
                gradient = 1000;
                break;
            case 2:
                min = 1;
                max = 30;
                gradient = 1;
                break;
            case 3:
                min = 1;
                max = 50;
                gradient = 1;
                break;
            case 4:
                min = 5;
                max = 100;
                gradient = 5;
                break;
        }
        int size = (max - min) / gradient;
        for (int i = 0; i <= size; i++) {
            int number = min + i * gradient;
            if (selectInt == number) {
                selectIndex = i;
            }
            list.add(number + "");
        }
    }

    public SetTargetDialog builder() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_set_target, null, false);
        LoopView loopView = view.findViewById(R.id.loopView);
        TextView tv_sure = view.findViewById(R.id.tv_sure);
        loopView.setItems(list);
        loopView.setNotLoop();
        loopView.setCurrentPosition(selectIndex);

        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                selectIndex = index;
            }
        });


        BaseCustomDialog.Builder builder = new BaseCustomDialog.Builder(context);
        dialog = builder
                .style(R.style.dialogstyle)
                .cancelTouchout(true)
                .view(view)
                .gravity(Gravity.BOTTOM)            //不传默认底部
                .build();

        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSelect != null)
                    onSelect.onSelect(list.get(selectIndex));
                dialog.dismiss();
            }
        });

        if (!((Activity) context).isDestroyed() && dialog != null) {
            dialog.show();
        }
        return this;
    }

    public void show() {
        if (!((Activity) context).isDestroyed() && dialog != null) {
            dialog.show();
        }
    }

    public interface OnSelect {
        void onSelect(String str);
    }
}
